<template>
	<view class="ys">
		<view class="form">
			<uni-section title="派单选项" type="line">
				<view class="inputbox">
					<uni-data-select v-model="type" :localdata="typeRange"></uni-data-select>
				</view>
			</uni-section>
			<!-- <uni-section title="房间订单" type="line">
				<view class="inputbox">
					<uni-data-select v-model="order_id" :localdata="orderRange" @change="changeOrder"></uni-data-select>
				</view>
			</uni-section> -->
			<uni-section title="人员列表" type="line">
				<view class="inputbox">
					<uni-data-select v-model="staff_id" :localdata="staffRange"></uni-data-select>
				</view>
			</uni-section>
			<uni-section title="派单金额" type="line">
				<view class="inputbox">
					<uni-easyinput v-model="money" trim placeholder="请输入派单金额" clearSize="20"></uni-easyinput>
				</view>
			</uni-section>
			<uni-section title="备注" type="line">
				<view class="inputbox">
					<uni-easyinput v-model="content" trim placeholder="请输入派单备注" clearSize="20"></uni-easyinput>
				</view>
			</uni-section>
		</view>

		<button class="btn" @click="addPenalty" :loading="loading" :disabled="loading">提交</button>
	</view>
</template>

<script>
	import {
		getPenaltyOptions,
		getFinishRoomOrder,
		addPenalty,
		getStaff
	} from "@/api/user.js"
	export default {
		data() {
			return {
				type: '',
				typeRange: [],
				order_id: '',
				orderRange: [],
				orderList: [],
				staff_id: '',
				staffRange: [],
				money: '',
				loading: false,
				content: '',
				stafflist: []
			}
		},
		onLoad() {
			this.getStaff()
			this.getPenaltyOptions()
		},
		methods: {
			getStaff() {
				getStaff().then(res => {
					if (res.code == 0) {
						let data = res.data.list
						this.staffRange = data.map(function(item) {
							return {
								text: `${item.name}-${item.role.text}-${item.phone}`,
								value: item.id
							}
						})
						this.stafflist = res.data.list
					}
				})
			},
			getFinishRoomOrder() {
				getFinishRoomOrder().then(res => {
					if (res.code == 0) {
						let data = res.data
						this.orderList = data
						this.orderRange = data.map(function(item) {
							return {
								text: `${item.room.name},完成:${item.finish_time}`,
								value: item.id
							}
						})
					}
				})
			},
			getPenaltyOptions() {
				getPenaltyOptions().then(res => {
					if (res.code == 0) {
						let data = res.data
						this.typeRange = data
					}
				})
			},
			changeOrder(e) {
				let index = this.orderList.findIndex(item => item.id == e)
				if (index > -1) {
					let staff = this.orderList[index].staff
					this.staffRange = staff.map(function(sf) {
						return {
							value: sf.staff_id,
							text: `${sf.staff.name}(${sf.staff.phone})`
						}
					})
				}
			},
			addPenalty() {
				let that = this
				if (this.type == '') {
					return uni.$showMsg('请选择派单类别')
				}
				// if (this.order_id == '') {
				// 	return uni.$showMsg('请选择房间订单')
				// }
				if (this.staff_id == '') {
					return uni.$showMsg('请指定派单人员')
				}
				if (this.money == '') {
					return uni.$showMsg('请输入派单金额')
				}
				that.loading = true
				addPenalty({
					type: this.type,
					// order_id: this.order_id,
					staff_id: this.staff_id,
					money: this.money,
					remarks: this.content
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					} else {
						that.loading = false
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.ys {
		.inputbox {
			width: 96%;
			margin: 0px auto;
		}

		.form {
			padding-bottom: 15px;
			background: #fff;
		}

		.btn {
			button {
				color: #fff;
				font-size: 16px;
				letter-spacing: 1px;
				margin: 30px auto;
				width: 96%;
			}
		}
	}
</style>